<?php

namespace Analysis\Model;

use Think\Model;

class StaticModel extends Model
{
    protected $autoCheckFields = false;

    /** 参数 * */
    private $start;
    private $end;
    private $field;

    /** 图表数据 * */
    private $cat;

    public function getInitialParam($s, $e, $field)
    {
        $this->start = $s;
        $this->end = $e;
        $this->field = $field;

        $daily = ($e - $s) / 24 / 3600;
        if ($daily <= 31) {
            return $this->getDailySeleInfo();
        }
        if ($daily <= 730) {
            return $this->getMonthSeleInfo();
        }
        return $this->getYearSeleInfo();
    }

    /*
     * 按日查询统计
     */

    private function getDailySeleInfo()
    {
        $count = ($this->end - $this->start) / 24 / 3600;
        $arr = array();
        for ($i = 0; $i <= $count; $i++) {
            $st = $this->start + $i * 24 * 3600;
            $arr["menu"][$i] = date("m/d", $st);
            $arr[$i][$this->field] = array("between", array($st, $st + 24 * 3600 - 1));
        }
        return $arr;
    }

    /*
     * 按月查询统计
     */

    private function getMonthSeleInfo()
    {
        $count = abs(date("Y", $this->end) - date("Y", $this->start)) * 12 + abs(date("m", $this->end) - date("m", $this->start));
        $s_month_last = strtotime(date("Y-m-t", $this->start));
        $e_month_first = strtotime(date("Y-m-1", $this->end));
        $arr = array();
        for ($i = 0; $i <= $count; $i++) {
            $month_s = strtotime("+" . $i . " month", $this->start);
            $arr["menu"][$i] = date("Ym", $month_s);
            $comm = array("between", array(strtotime(date("Y-m-1", $month_s)), strtotime(date("Y-m-t", $month_s))));
            if ($i == 0) {
                $arr[$i][$this->field] = array("between", array($this->start, $s_month_last));
            } else {
                $arr[$i][$this->field] = $i == $count ? array("between", array($e_month_first, $this->end)) : $comm;
            }
        }
        return $arr;
    }

    /*
     * 按年查询统计
     */

    private function getYearSeleInfo()
    {
        $count = abs(date("Y", $this->end) - date("Y", $this->start));
        $arr = array();
        $start = date("Y", $this->start);
        for ($i = 0; $i <= $count; $i++) {
            $year = $start + $i;
            $arr["menu"][$i] = $year;
            if ($i == 0) {
                $arr[$i][$this->field] = array("between", array($this->start, strtotime(date($year . "-12-31 23:59:59"))));
            } else {
                $comm = array("between", array(strtotime(date($year . "-1-1")), strtotime(date($year . "-12-31 23:59:59"))));
                $arr[$i][$this->field] = $i == $count ? array("between", array(strtotime(date($year . "-1-1")), $this->end + 24 * 3600 - 1)) : $comm;
            }
        }
        return $arr;
    }

    /*
     * 查询统计订单数据
     */

    public function seleAndCountData($dbs, $where)
    {
        $orderTotal = array();
        for ($i = 0; $i < count($where); $i++) {
            //$where[$i]["dll_status"] = 1;
            $orderTotal["nums"][$i] = intval($dbs->where($where[$i])->count());
            $temp = $dbs->where($where[$i])->sum("dll_amount");
            $orderTotal["gold"][$i] = $temp == null ? 0 : $temp;
        }
        return $orderTotal;
    }

    /*
     * 查询统计搜索数据
     */

    public function pollAndCountData($dbs, $where, $isbn)
    {
        $orderTotal = array();
        for ($i = 0; $i < count($where); $i++) {
            $where[$i]["pol_cod"] = $isbn;
            $orderTotal["nums"][$i] = intval($dbs->where($where[$i])->count());
        }
        return $orderTotal;
    }
    
    /*
     * 按需查询数据
     */
    public function reportSerchData($dbs, $where, $po)
    {
        $orderTotal = array();
        for ($i = 0; $i < count($where); $i++) {
            $where[$i]["ord_status"] = $po["ord_status"];
            $where[$i]["ord_sn"] = $po["ord_sn"];
            $orderTotal["nums"][$i] = intval($dbs->where($where[$i])->count());
            $temp = $dbs->where($where[$i])->sum("ord_amount");
            $orderTotal["gold"][$i] = $temp == null ? 0 : $temp;
        }
        return $orderTotal;
    }
}
